//
//  NSArray+MB.m
//  MBiOSKit
//
//  Created by catch on 2016/9/26.
//  Copyright © 2016年 catch. All rights reserved.
//

#import "NSArray+MB.h"

@implementation NSArray (MB)

- (instancetype)initWithObjects_safe:(id *)objects count:(NSUInteger)cnt {
    NSUInteger newCnt = 0;
    for (NSUInteger i = 0; i < cnt; i++) {
        if (!objects[i]) {
            NSLog(@"ERROR -- NSArray initWithObjects_safe");
            break;
        }
        newCnt++;
    }
    self = [self initWithObjects:objects count:newCnt];
    return self;
}

- (id)safe_objectAtIndex:(NSUInteger)index {
    if (index >= [self count]) {
        NSLog(@"ERROR -- NSArray safe_objectAtIndex");
        return nil;
    }
    return [self objectAtIndex:index];
}

- (NSArray *)safe_arrayByAddingObject:(id)anObject {
    if (!anObject) {
        NSLog(@"ERROR -- NSArray safe_arrayByAddingObject");
        return self;
    }
    return [self arrayByAddingObject:anObject];
}


@end
